package com.example.qxfw.csnl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.qxfw.common.entity.zy.BaseProduct;
import com.example.qxfw.common.entity.zy.WaterLoggingFcst;
import com.example.qxfw.common.entity.zy.WaterLoggingPointFcst;
import com.example.qxfw.common.entity.zy.WaterLoggingPointInfo;
import com.example.qxfw.mongodb.hy.entity.HyDocFile;
import com.example.qxfw.csnl.mapper.WaterLoggingFcstMapper;
import com.example.qxfw.csnl.mapper.WaterLoggingPointFcstMapper;
import com.example.qxfw.csnl.mapper.WaterLoggingPointInfoMapper;
import com.example.qxfw.csnl.service.NllsService;
import com.example.qxfw.ybzz.mapper.BaseProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

@Service
public class NllsServiceImpl implements NllsService {
    @Autowired
    private WaterLoggingFcstMapper waterLoggingFcstMapper;
    @Autowired
    private WaterLoggingPointInfoMapper waterLoggingPointInfoMapper;
    @Autowired
    private WaterLoggingPointFcstMapper waterLoggingPointFcstMapper;

    @Autowired
    private BaseProductMapper baseProductMapper;

    @Qualifier("mongoSecondTemplate")
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public List<WaterLoggingFcst> selectOfWaterLoggingFcst(String startTime, String endTime) {
        Timestamp start = null;
        Timestamp end = null;
        if (startTime!=null || startTime.isEmpty()){
            start = Timestamp.valueOf(startTime);
        }
        if (endTime!=null || endTime.isEmpty()){
            end = Timestamp.valueOf(endTime);
        }

        return waterLoggingFcstMapper.selectList(new LambdaQueryWrapper<WaterLoggingFcst>()
                .between(WaterLoggingFcst::getMaketime, start, end)
                .orderBy(true,false,WaterLoggingFcst::getId));
    }

    @Override
    public Integer getProductId(Integer docId, String docPath) {
        List<BaseProduct> baseProducts = baseProductMapper.selectList(Wrappers.<BaseProduct>lambdaQuery()
                .eq(BaseProduct::getBindid, docId)
                .eq(BaseProduct::getProductname, docPath));
        if (baseProducts.size()>0){
            return baseProducts.get(0).getId();
        } else {
            return null;
        }

    }

    @Override
    public WaterLoggingFcst deleteProduct(Integer docId) {
        WaterLoggingFcst waterLoggingFcst = waterLoggingFcstMapper.selectById(docId);
        waterLoggingFcstMapper.deleteById(docId);
        if (waterLoggingFcst!=null && StringUtils.isNotBlank(waterLoggingFcst.getDocpath())){
            List<BaseProduct> baseProducts = baseProductMapper.selectList(Wrappers.<BaseProduct>lambdaQuery()
                    .eq(BaseProduct::getBindid, docId)
                    .eq(BaseProduct::getProductname, waterLoggingFcst.getDocpath()));
            if (baseProducts.size()>0){
                baseProductMapper.delete(Wrappers.<BaseProduct>lambdaQuery()
                        .eq(BaseProduct::getBindid, docId)
                        .eq(BaseProduct::getProductname, waterLoggingFcst.getDocpath()));
                mongoTemplate.remove(new Query(
                        Criteria.where("id").is(baseProducts.get(0).getId())
                                .and("table_name").is("base_product")), HyDocFile.class);
            }
        }
        return waterLoggingFcst;
    }

    @Override
    public List<WaterLoggingPointInfo> viewDetails(int id) {
        List<WaterLoggingPointInfo> list1 = new ArrayList<>();
        List<WaterLoggingPointFcst> list = waterLoggingPointFcstMapper.selectList(new LambdaQueryWrapper<WaterLoggingPointFcst>()
                .eq(WaterLoggingPointFcst::getWarningid, id));
        for (WaterLoggingPointFcst pointFcst : list) {
            WaterLoggingPointInfo waterLoggingPointInfo = waterLoggingPointInfoMapper.selectById(new LambdaQueryWrapper<WaterLoggingPointInfo>()
                    .eq(WaterLoggingPointInfo::getId, pointFcst.getWaterloggingpointid()));
            list1.add(waterLoggingPointInfo);
        }

        return list1;


    }
}
